package com.tianbing.adlottery.controller.hordeadmin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.fengniu.ejb.business.ICategoryBusiness;
import com.fengniu.ejb.business.IContentBusiness;
import com.fengniu.ejb.business.IUserBusiness;
import com.fengniu.ejb.dao.ICategoryDao;
import com.fengniu.ejb.dao.IUserDao;
import com.fengniu.ejb.entity.Category2Category;
import com.fengniu.ejb.entity.Content;
import com.fengniu.ejb.entity.Content2Category;
import com.fengniu.ejb.entity.User2Content;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;

public class EditMyServedProductProfileController extends SimpleFormController {
	static Logger logger = Logger.getLogger(EditMyServedProductProfileController.class.getName());

	private IUserDao userDao;

	private SkinUtils skinUtils;

	private ICategoryDao categoryDao;

	private IUserBusiness userBusiness;

	private ICategoryBusiness categoryBusiness;

	private IContentBusiness contentBusiness;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		ControllerFormBean fb = new ControllerFormBean();
		long u2csid = ServletRequestUtils.getLongParameter(request, "u2csid", -1);
		String cgid = ServletRequestUtils.getStringParameter(request, "cgid", null);

		User2Content u2c = null;
		if (u2csid > 0) {
			u2c = userDao.findUser2ContentBy(u2csid);
		}
		if (u2c == null) {
			Content c = new Content();
			u2c = new User2Content();
			u2c.setContent(c);
		}
		fb.setUser2Content(u2c);
		// fb.setCategoryId(cgid);
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {
		String cgid = ServletRequestUtils.getStringParameter(request, "cgid", null);
		String brandCategoryId = ServletRequestUtils.getStringParameter(request, "brandCategoryId", null);

		UserSession auth = skinUtils.getUserAuthorization(request, null);
		ModelAndView mv = new ModelAndView(this.getSuccessView());
		ControllerFormBean form = (ControllerFormBean) cmd;
		User2Content s2c = form.getUser2Content();
		userBusiness.saveUserServedProduct(auth.getUserSysId(), s2c.getContent(), cgid, brandCategoryId);
		return mv;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();

		UserSession auth = skinUtils.getUserAuthorization(request, null);
		String cgid = ServletRequestUtils.getStringParameter(request, "cgid", null);
		if (cgid != null) {
			List primaryContentCategoryTrail = categoryDao.getListCategoryPrimaryTrailBy(cgid);
			if (primaryContentCategoryTrail != null && primaryContentCategoryTrail.size() > 0) {
				model.put("primaryContentCategoryTrail", primaryContentCategoryTrail);
				model.put("cgid", cgid);
			}
			// get cgid related brand categories
			List<Category2Category> g2gBrandList = categoryBusiness.getListCategoryRelatedBrand(cgid);
			if (g2gBrandList != null && g2gBrandList.size() > 0) {
				model.put("g2gBrandList", g2gBrandList);
			}
		}
		long csid = ServletRequestUtils.getLongParameter(request, "csid", -1);
		if (csid > -1) {
			// get coupon brand category
			Content2Category content2BrandCategory = contentBusiness.findContent2BrandCategory(csid);
			model.put("content2BrandCategory", content2BrandCategory);
		}

		return model;
	}

	public static void setLogger(Logger logger) {
		EditMyServedProductProfileController.logger = logger;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public void setCategoryDao(ICategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public void setUserBusiness(IUserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

	public void setCategoryBusiness(ICategoryBusiness categoryBusiness) {
		this.categoryBusiness = categoryBusiness;
	}

	public void setContentBusiness(IContentBusiness contentBusiness) {
		this.contentBusiness = contentBusiness;
	}

}
